<template>
  <el-dialog
    :close-on-click-modal="false"
    title="招标文本"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="120px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="招标编号" prop="bidNumber">
            <!-- <el-input v-model="form.bidNumber" :disabled="disableForm" /> -->
            <el-input v-model="form.bidNumber" readonly :disabled="disableForm || optType !== 'add'" @click.native="optType === 'add' ? showListModal = true : null">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标名称" prop="bidName">
            <el-input v-model="form.bidName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标机构" prop="bidOrganization">
            <el-input v-model="form.bidOrganization" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标类型" prop="bidType">
            <el-select v-model="form.bidType" disabled style="width: 100%;" placeholder="请选择招标类型">
              <el-option v-for="item in bidTextInfoBidType" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标负责人" prop="basBidPerson">
            <el-input v-model="form.basBidPerson" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="客商等级" prop="basQualificationLevel">
            <el-select v-model="form.basQualificationLevel" placeholder="请选择">
              <el-option label="A" value="1" />
              <el-option label="B" value="2" />
              <el-option label="C" value="3" />
              <el-option label="D" value="4" />
            </el-select>
            <!-- <el-input v-model="form.basQualificationLevel" :disabled="disableForm" /> -->
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="注册资金(万元)" prop="basRegisteredCapital">
            <el-input v-model="form.basRegisteredCapital" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="投标保证金(万元)" prop="basBidMargin">
            <el-input v-model="form.basBidMargin" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标文本费" prop="basTextAmount">
            <el-input v-model="form.basTextAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标完成时间" prop="basBidFinishTime">
            <el-date-picker v-model="form.basBidFinishTime" style="width:100%;" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="描述" prop="basDescription">
            <el-input v-model="form.basDescription" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-button type="primary" @click="showInfoList">引入</el-button>

      <el-button type="primary" @click="addLis()">新增</el-button>
      <com-table
        v-model="selectRow"
        highlight-current-row
        :data="targetList"
        :columns="columns"
        :no-page="true"
        row-key="number"
        style="margin-top:20px"
      >
        <template v-slot:supName="{row}">
          <el-input v-model="row.supName" />
        </template>
        <template v-slot:supNumber="{row}">
          <el-input v-model="row.supNumber" />
        </template>
        <template v-slot:supFraction="{row}">
          <el-input v-model="row.supFraction" />
        </template>
        <template v-slot:del="{row}">
          <el-button
            type="text"
            size="small"
            @click.native.prevent="deleteRow(row)"
          >
            移除
          </el-button>
        </template>
      </com-table>
      <div class="com-form-title">附件</div>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
    <infoList title="供应商列表" :show-modal.sync="showList1" :columns="columns" :api-obj="{api:getSupplierAssessFinishInfoList, params:infoListParam}" @getSelectedRows2="getSelectedRows2" />

    <listSelect :show-modal.sync="showListModal" title="招标限价选择" :columns="listColumns" :api="getBidLimitFinishInfoList" @getSelectedData="getSelectedRows" />
  </el-dialog>

</template>

<script>
import { getBidLimitFinishInfoList, addBidTextInfo, updateBidTextInfo, getBidTextInfoDetail, getSupplierAssessFinishInfoList } from '@/api/bigManage'
import { bidTextInfoBidType, workflowStatus } from '@/utils/dicData'
import { getDictValue } from '@/utils/comm'
import fileUploader from '@/components/FileUploader'
import listSelect from '@/components/ListSelect'
import infoList from './infoList.vue'

export default {
  name: 'AddBidTextInfo',
  components: {
    fileUploader,
    listSelect,
    infoList
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      getSupplierAssessFinishInfoList,
      getBidLimitFinishInfoList,
      showFileModal: false,
      showListModal: false,
      showList: false,
      showList1: false,
      selectRow: null,
      targetList: [],
      bidTextInfoBidType,
      workflowStatus,
      getDictValue,
      infoListParam: {},
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        bidInfoId: '',
        bidOrganization: '',
        bidNumber: '',
        bidName: '',
        bidType: '',
        basTextAmount: '',
        basBidFinishTime: '',
        basBidPerson: '',
        basDescription: '',
        basQualificationLevel: '4',
        basRegisteredCapital: '',
        basBidMargin: ''
      },
      fileList: [
      ],
      listColumns: [
        {
          label: '招标机构',
          prop: 'bidOrganization'
        },
        {
          label: '招标编号',
          prop: 'bidNumber',
          width: '160'
        },
        {
          label: '招标名称',
          prop: 'bidName',
          width: '150'
        },
        {
          label: '招标方式',
          prop: 'bidMode'
        },
        {
          label: '招标类型',
          prop: 'bidType'
        },
        {
          label: '限价(元)',
          prop: 'basLimitPrice'
        },
        {
          label: '备注',
          prop: 'basRemark'
        },
        {
          label: '编制人',
          prop: 'founderName'
        },
        {
          label: '编制时间',
          prop: 'createTime',
          width: '90'
        }
      ],
      columns: [
        {
          label: '客商名称',
          prop: 'supName',
          slot: 'supName'

        },
        {
          label: '客商编号',
          prop: 'supNumber',
          slot: 'supNumber'

        },
        {
          label: '分数',
          prop: 'supFraction',
          slot: 'supFraction'
        },
        {
          label: '操作',
          prop: 'del',
          slot: 'del'
        }
      ],
      rules: {
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            const source = this.data[key]
            if (typeof source === 'boolean') {
              this.form[key] = source ? 1 : 0
            } else {
              this.form[key] = source
            }
          })
        })
      } else if (!val) {
        this.targetList = []
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
  },
  methods: {
    showInfoList() {
      this.infoListParam = {
        'rating': this.form.basQualificationLevel
      }
      this.showList1 = true
    },
    addLis() {
      this.targetList.push({
        supName: '',
        supNumber: '',
        supFraction: ''
      })
      this.targetList.forEach((item, index) => {
        item.index = index
      })
    },
    getSelectedRows2(data) {
      this.targetList.push({
        supName: data.spiName,
        supNumber: data.saiNumber,
        supFraction: data.saiFraction
      })
    },
    deleteRow(row) {
      this.targetList.splice(row.index, 1)
    },
    getSelectedRows(data) {
      if (data) {
        // this.supplierInfo = data
        const { bidName, bidNumber, bidOrganization, bidType, bidInfoId } = data
        this.form.bidInfoId = bidInfoId
        this.form.bidName = bidName
        this.form.bidNumber = bidNumber
        this.form.bidOrganization = bidOrganization
        this.form.bidType = this.getDictValue(this.bidTextInfoBidType, bidType)
      }
    },
    getDocDetail(id) {
      getBidTextInfoDetail({ bidTextInfoId: id }).then(res => {
        const { lstAttachment, lstBidTextDetail } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        this.targetList = lstBidTextDetail
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            bidTextInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList,
            lstBidTextDetail: this.targetList
          }
          const fn = this.optType === 'add' ? addBidTextInfo : updateBidTextInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
